package com.vastcom.cms.action;

import com.vastcom.cms.service.ManageService;
import com.vastcom.cms.tools.JwtUtil;
import com.vastcom.cms.tools.Tools;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;

@CrossOrigin
@RestController
@RequestMapping("/auth")
public class AuthAction {

    @Autowired
    private ManageService manageService;

    @GetMapping("refreshToken")
    @ResponseBody
    public HashMap refreshToken() throws Exception{
        HashMap m=new HashMap();

        try {
            String userid=manageService.getCurrentUserId();
            String role=manageService.getCurrentRole();

            if(Tools.isNullOrSpace(userid)||Tools.isNullOrSpace(role)){
                m.put("msg","errortoken");
            }
            else{
                m.put("msg","ok");
                String token= JwtUtil.generateToken(role,userid,1000*60*60);
                m.put("token",token);

            }
        } catch (Exception e) {
            e.printStackTrace();
            m.put("msg","error");
        }

        return m;
    }

    @GetMapping("getRole")
    @ResponseBody
    public HashMap getRole() throws Exception{
        HashMap m=new HashMap();
        try {
            String role=manageService.getCurrentRole();
            if(!Tools.isNullOrSpace(role)){
                m.put("msg","ok");
                m.put("role",role);
            }
            else {
                m.put("msg","roleerror");
            }
        } catch (Exception e) {
            e.printStackTrace();
            m.put("msg","error");
        }
        return m;
    }

}
